<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
       /*  $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $this->view->assign([
            'totaluser'        => 35200,
            'totalviews'       => 219390,
            'totalorder'       => 32143,
            'totalorderamount' => 174800,
            'todayuserlogin'   => 321,
            'todayusersignup'  => 430,
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'uploadmode'       => $uploadmode
        ]); */
		
		$sql = "select count(a.id) as num
				from fa_admin as a, fa_auth_group_access as b, fa_auth_group as c
				where a.id = b.uid and b.group_id = c.id and a.`status` = 'normal' and c.`status` = 'normal' and c.pid not in ('0','1');";
		$row = Db::query($sql);
		// 账号数
		$total_account_num = 0;
		if($row[0]['num']) {
			$total_account_num = $row[0]['num'];
		}
		$this->view->assign('total_account_num', $total_account_num);
		
		$sql = "select count(*) as num, salestatus from fa_salemanagement group by salestatus";
		$row = Db::query($sql);
		$instrument_data = array(
			'saled' => 0,
			'unsale' => 0,
			'total' => 0
		);
		if(!empty($row)){
			foreach($row as $v) {
				if($v['salestatus'] == '0') {
					$instrument_data['unsale'] = $v['num'];					
				}
				if($v['salestatus'] == '1') {
					$instrument_data['saled'] = $v['num'];					
				}
				$instrument_data['total'] = $instrument_data['unsale'] + $instrument_data['saled'];
			}
		}
		$this->view->assign('instrument_data', $instrument_data);
		

        // 饼图数据
        $pieData = $this->getInstrumentFileNum();
		
		$totalFileNumber = 0;
		if(!empty($pieData)) {
			foreach($pieData as $v) {
				$totalFileNumber += $v['num'];
			}
		}
		$this->view->assign('totalFileNumber', $totalFileNumber);
        $this->view->assign('pieData', $pieData);
		
        // 条状图数据
        $instrumentSetting = $this->getInstrumentSetting();
        $barData = $this->getInstrumentCapacity();
		
		// 整体空间使用率
		$totalSpaceUseRate = array(
			'x' => 0,
			'y' => 0
		);
        $insTemp = array();
        $x = 1024 * 1024;
        if(count($instrumentSetting)) {
            foreach($instrumentSetting as $v) {
                $insTemp[$v['name']] = $v['space'];
				$totalSpaceUseRate['y'] += $v['space'];
            }
        }
        
        $barArr = array();
        if(count($barData)) {
            foreach($barData as $v) {
                $_used = 0;
                $_left = $insTemp[$v['name']];

                if($v['num'] > 0) {
                    $_used = round($v['num'] / $x, 2);
                    $_left = round($insTemp[$v['name']] - $_used, 2);
                }
                $barArr[$v['name']] = array(
                    'used' => $_used,
                    'left' => $_left
                );
				
				$totalSpaceUseRate['x'] += $_used;
            }
        }
        $this->view->assign('barArr', $barArr);
		$spaceUseRate = '0%';
		if($totalSpaceUseRate['y']) {
			$spaceUseRate = round(($totalSpaceUseRate['x'] / $totalSpaceUseRate['y']) * 100, 2).' %';			
		}		
		$this->view->assign('spaceUseRate', $spaceUseRate);
		
		
        return $this->view->fetch();
    }

    // 每个仪器下的文件数量
    public function getInstrumentFileNum() 
    {
        $where = array(
            'admin_id' => $this->auth->id,
            'state' => '1', // 正常
        );

        $res = Db::name('filedata')->field('instumentname as name, instumenttype as type, sum(filenum) as num')->where($where)->group('instumentname')->select();

        return $res;
    }

    // 每个仪器下的文件数量
    public function getInstrumentCapacity() 
    {
        $where = array(
            'admin_id' => $this->auth->id,
            'state' => '1', // 正常
        );

        $res = Db::name('filedata')->field('instumentname as name, instumenttype as type, sum(filecapacity) as num')->where($where)->group('instumentname')->select();
        
        return $res;
    }

    // 获得仪器的配置信息
    public function getInstrumentSetting(){
        $where = array(
            'state' => '1'
        );
        $res = Db::name('instrument')->where($where)->field('title as name, sum(storagespace) as space, sum(maxfilenum) as file')->group('title')->select();

        return $res;
    }

}
